package com.itheima.Exception;

import com.itheima.common.CateException;
import com.itheima.common.R;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice(annotations = {RestController.class, Controller.class})//添加了这些注解的类才会走这个异常处理
public class GlobalExceptionHandler {

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R SqlEx(SQLIntegrityConstraintViolationException exception){
        //所有的表的name字段都是unique，所以每个类重复加入都是会走这个处理
        /*return R.error("用户已存在");*/
        return R.error("已存在");
    }

    @ExceptionHandler(CateException.class)
    public R CategoryEx(CateException e){
        return R.error(e.getMessage());
    }
}
